<?php

namespace app\demo\controller;

use think\App;
use think\facade\Db;

class Code extends Base
{

    public function getSqlField($db_name, $table_name)
    {
        $sql = <<<EOL
SELECT column_name, column_comment,data_type,character_maximum_length 
FROM information_schema.columns
WHERE table_schema = '$db_name' AND table_name = '$table_name' AND column_name NOT IN ('id','del','create_time','update_time')
EOL;
        return Db::query($sql);
    }

    public function autoCode()
    {
        $db_name = "heat";
        $table_name = "work_order";
        $list = $this->getSqlField($db_name, $table_name);
        $html = <<<EOD

EOD;
        $php = <<<EOD
<?php
\$param = request()->post();

EOD;
        $php2 = <<<EOD
<?php
\$array = [


EOD;
        $js = <<<EOD
<script>
const formData = reactive({

EOD;
        $js2 = <<<EOD
<script>

EOD;
        foreach ($list as $row) {

            $php .= <<<EOD
\$post['{$row['column_name']}'] = \$param['{$row['column_name']}'] ?? '';

EOD;
            $php2 .= <<<EOD
['title' => '{$row['column_comment']}', 'field' => '{$row['column_name']}'],

EOD;
            $js .= <<<EOD
    {$row['column_name']}: '',

EOD;
            $js2 .= <<<EOD
    formData.{$row['column_name']} = res.{$row['column_name']}

EOD;

            if ($row['data_type'] === 'varchar') {
                $html .= <<<EOD
<el-form-item prop="{$row['column_name']}" label="{$row['column_comment']}">
    <el-input v-model.trim="formData.{$row['column_name']}" maxlength="{$row['character_maximum_length']}" placeholder="请输入"></el-input>
</el-form-item>

EOD;
            }

            if ($row['data_type'] === 'date') {
                $html .= <<<EOD
<el-form-item prop="{$row['column_name']}" label="{$row['column_comment']}">
    <el-date-picker type="date" value-format="YYYY-MM-DD" v-model="formData.{$row['column_name']}" placeholder="请选择"></el-date-picker>
</el-form-item>

EOD;
            }
            if ($row['data_type'] === 'datetime') {
                $html .= <<<EOD
<el-form-item prop="{$row['column_name']}" label="{$row['column_comment']}">
    <el-date-picker type="datetime" v-model="formData.{$row['column_name']}" placeholder="请选择"></el-date-picker>
</el-form-item>

EOD;
            }
        }
        $php .= <<<EOD
checkPost(\$post);
?>

EOD;
        $php2 .= <<<EOD
];
?>

EOD;
        $js .= <<<EOD
})
</script>

EOD;
        $js2 .= <<<EOD

</script>

EOD;



        writeFile($html, $table_name . '.html');
        writeFile($php, $table_name . '.php');
        writeFile($php2, $table_name . '.php');
        writeFile($js, $table_name . '.html');
        writeFile($js2, $table_name . '.html');

        pp('success');
    }


}
